﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Xpo;
using BusinessObjects;

namespace UI
{
    public partial class frmTinhThanh : FormBase
    {
        public frmTinhThanh()
        {
            InitializeComponent();
        }

        private void gcTinhThanh_Click(object sender, EventArgs e)
        {

        }

        private void gcTinhThanh_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Edit();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void Edit()
        {
            if (gvTinhThanh.GetFocusedRow() != null)
            {
                using (NestedUnitOfWork nuow = xpCollectionTinhThanh.Session.BeginNestedUnitOfWork())
                {
                    var obj = nuow.GetNestedObject(xpCollectionTinhThanh[gvTinhThanh.GetDataSourceRowIndex(gvTinhThanh.FocusedRowHandle)]) as TinhThanh;
                    frmTinhThanh_Edit frm = new frmTinhThanh_Edit();
                    frm.ItemEdit = obj;
                    if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        nuow.CommitChanges();
                }
             }
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                using (NestedUnitOfWork nuow = xpCollectionTinhThanh.Session.BeginNestedUnitOfWork())
                {
                    frmTinhThanh_Edit frm = new frmTinhThanh_Edit();
                    frm.ItemEdit = new TinhThanh(nuow);
                    if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        nuow.CommitChanges();
                        xpCollectionTinhThanh.Reload();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Edit();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (gvTinhThanh.GetFocusedRow() != null)
                {
                    if (CommonConfirm("Bạn có chắc xóa tỉnh thành này?", "Lưu ý!"))
                    {
                        using (NestedUnitOfWork nuow = xpCollectionTinhThanh.Session.BeginNestedUnitOfWork())
                        {
                            var obj = nuow.GetNestedObject(xpCollectionTinhThanh[gvTinhThanh.GetDataSourceRowIndex(gvTinhThanh.FocusedRowHandle)]);

                            nuow.Delete(obj);
                            nuow.CommitChanges();
                        }
                        xpCollectionTinhThanh.Reload();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }
    }
}
